package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.GoodsDescribeEntity;
import com.yimils.core.goods.entity.GoodsPictureEntity;
import com.yimils.core.goods.entity.PictureEntity;
import com.yimils.core.goods.mapper.GoodsDescribeMapper;
import com.yimils.core.goods.serivce.GoodsDescribeService;
import com.yimils.core.goods.serivce.PictureService;
import com.yimils.core.goods.vo.GoodsPictureRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsDescribeService")
public class GoodsDescribeServiceImpl extends ServiceImpl<GoodsDescribeMapper, GoodsDescribeEntity> implements GoodsDescribeService {
    @Resource(name = "pictureService")
    PictureService pictureService;
    @Override
    public Boolean saveGoodsPicture(List<String> describeUrl, Long goodsId) {
        List<PictureEntity> entities = pictureService.savePictureList(describeUrl);
        List<GoodsDescribeEntity> collect = entities.stream().map(o -> {
            GoodsDescribeEntity goodsDescribeEntity = new GoodsDescribeEntity();
            goodsDescribeEntity.setGoodsId(goodsId);
            goodsDescribeEntity.setDescribeId(o.getId());
            goodsDescribeEntity.setState(Boolean.FALSE);
            goodsDescribeEntity.setCreateTime(LocalDateTime.now());
            goodsDescribeEntity.setUpdateTime(LocalDateTime.now());
            return goodsDescribeEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Boolean deleteGoodsPicture(List<Long> describeIds, Long goodsId) {
        QueryWrapper<GoodsDescribeEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsDescribeEntity::getGoodsId, goodsId)
                .eq(GoodsDescribeEntity::getState, Boolean.FALSE);
        List<GoodsDescribeEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        saveOrUpdateBatch(entities);
        List<GoodsDescribeEntity> collect = describeIds.stream().map(o -> {
            GoodsDescribeEntity goodsPictureEntity = new GoodsDescribeEntity();
            goodsPictureEntity.setGoodsId(goodsId);
            goodsPictureEntity.setDescribeId(o);
            goodsPictureEntity.setState(Boolean.FALSE);
            goodsPictureEntity.setCreateTime(LocalDateTime.now());
            goodsPictureEntity.setUpdateTime(LocalDateTime.now());
            return goodsPictureEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Map<Long, List<GoodsPictureRes>> mapListGoodsPicture(List<Long> goodsIds) {
        QueryWrapper<GoodsDescribeEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsDescribeEntity::getGoodsId, goodsIds)
                .eq(GoodsDescribeEntity::getState, Boolean.FALSE);
        List<GoodsDescribeEntity> entities = list(qw);
        List<Long> pictureIds = entities.stream().map(GoodsDescribeEntity::getDescribeId).collect(Collectors.toList());
        List<PictureEntity> pictureEntities = pictureService.listPicture(pictureIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(PictureEntity::getId, PictureEntity::getUrl));
        return entities.stream().map(o -> {
            GoodsPictureRes res = new GoodsPictureRes();
            res.setGoodsId(o.getGoodsId());
            res.setPictureId(o.getDescribeId());
            res.setUrl(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsPictureRes::getGoodsId));
    }
}
